package com.matrix.calculator;

import java.util.ArrayList;

import com.matrix.math.ColumnVector;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ResultView extends Activity {

	private String[][] strmtrx;
	private int[] maxColLength;
    /** Called when the activity is first created. */
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		TableLayout aTable=(TableLayout)findViewById(R.id.matrix);
		//gets the information that was passes from the last activity
		Bundle b = this.getIntent().getExtras();
		//prints the result of the operations to the screen
		try{
			ArrayList<com.matrix.math.Matrix> resultMatrix = GlobalVars.getInstance().getResultMatrices();
			switch(b.getInt("OP"))
			{
			case 0: 
				//getMatrix gets on of the matrices out of the bundle
				//this.appendRows(aTable, resultMatrix.get(0));
				addMatrix(aTable, 0);
				break;
			case 5: 
				this.plu(aTable, resultMatrix);
				break;
			case 6: 
				addMatrix(aTable, 6);
				break;
			case 8:
				if(b.getInt("view")==0)
				{
					addMatrix(aTable, 0);
				}
				else if(b.getInt("view")==1)
				{
					addMatrix(aTable, 0);
				}
				else
				{
					addMatrix(aTable, 8);
				}
				break;
			default:
				addMatrix(aTable,10);
				
			}
		}catch(Exception e)
		{
			
		}
	}
	public void addMatrix(TableLayout table, int op)
	{
		TableRow row = new TableRow(this);
		TextView c = new TextView(this);
		
		ResultsDrawable r;
		ArrayList<ColumnVector> vList = GlobalVars.getInstance().getResultVectors();
		switch(op)
		{
		case 0:
			com.matrix.math.Matrix m = GlobalVars.getInstance().getResultMatrices().get(0);
			setStringMatrix(m);
			c.setText("Matrix "+m.getName());
			c.setPadding(3, 3, 3, 3);
			c.setTextSize(20);
			c.setTextColor(Color.BLACK);
			//adds the textView to the row
			row.addView(c);
			table.addView(row, new TableLayout.LayoutParams());
			row = new TableRow(this);
			r = new ResultsDrawable(this, strmtrx, maxColLength);
			row.addView(r, r.width()+25, r.height());
			table.addView(row, new TableLayout.LayoutParams());
			break;
		case 6:
			c.setText("Ax=b");
			c.setPadding(3, 3, 3, 3);
			c.setTextSize(20);
			c.setTextColor(Color.BLACK);
			//adds the textView to the row
			row.addView(c);
			table.addView(row, new TableLayout.LayoutParams());
			row = new TableRow(this);
			for(int i=0; i<vList.size(); i++)
			{
				ColumnVector v = GlobalVars.getInstance().getResultVectors().get(i);
				setStringVector(v);
				r = new ResultsDrawable(this, strmtrx, maxColLength);
				row.addView(r, r.width()+25, r.height());
				if(i<vList.size()-1)
				{
					c = new TextView(this);
					c.setText("+ C"+i);
					c.setPadding(3, 3, 3, 3);
					c.setTextSize(20);
					c.setTextColor(Color.BLACK);
					//adds the textView to the row
					row.addView(c);
					row.setGravity(Gravity.CENTER_VERTICAL);
				}
			}
			table.addView(row, new TableLayout.LayoutParams());
			break;
		case 8:
			ColumnVector v = GlobalVars.getInstance().getResultVectors().get(0);
			c.setText("Vector "+v.getName());
			c.setPadding(3, 3, 3, 3);
			c.setTextSize(20);
			c.setTextColor(Color.BLACK);
			//adds the textView to the row
			row.addView(c);
			table.addView(row, new TableLayout.LayoutParams());
			row = new TableRow(this);
			setStringVector(v);
			r = new ResultsDrawable(this, strmtrx, maxColLength);
			row.addView(r, r.width()+25, r.height());
			table.addView(row, new TableLayout.LayoutParams());
			break;
		default:
			c.setText("Null Vectors");
			c.setPadding(3, 3, 3, 3);
			c.setTextSize(20);
			c.setTextColor(Color.BLACK);
			//adds the textView to the row
			row.addView(c);
			table.addView(row, new TableLayout.LayoutParams());
			row = new TableRow(this);
			for(int i=0; i<vList.size(); i++)
			{
				v = GlobalVars.getInstance().getResultVectors().get(i);
				setStringVector(v);
				r = new ResultsDrawable(this, strmtrx, maxColLength);
				row.addView(r, r.width()+25, r.height());
			}
			table.addView(row, new TableLayout.LayoutParams());
		}
		
	}
	public void setStringMatrix(com.matrix.math.Matrix m)
	{
		String temp;
		strmtrx = new String[m.numRows()][m.numCols()];
		maxColLength = new int[m.numCols()];
		Rect r = new Rect();
		TextPaint tp = new TextPaint();
		int scaledSize = getResources().getDimensionPixelSize(R.dimen.font_size);
		tp.setTextSize(scaledSize);
		for(int i=0; i<strmtrx[0].length; i++)
		{
			maxColLength[i] =0;
			for(int j=0; j<strmtrx.length; j++)
			{
				try {
					temp = m.getValueAt(j, i)+""; 
					tp.getTextBounds(temp, 0, temp.length(), r);
					if(r.width()>maxColLength[i])
					{
						maxColLength[i] = r.width();
					}
					strmtrx[j][i] = temp;
					
				} catch (Exception e) {
				}
			}
		}
	}
	public void setStringVector(ColumnVector v)
	{
		String temp;
		strmtrx = new String[v.length()][1];
		maxColLength = new int[1];
		Rect r = new Rect();
		TextPaint tp = new TextPaint();
		int scaledSize = getResources().getDimensionPixelSize(R.dimen.font_size);
		tp.setTextSize(scaledSize);
		for(int i=0; i<strmtrx[0].length; i++)
		{
			maxColLength[i] =0;
			for(int j=0; j<strmtrx.length; j++)
			{
				try {
					temp = v.getValueAt(j)+""; 
					tp.getTextBounds(temp, 0, temp.length(), r);
					if(r.width()>maxColLength[i])
					{
						maxColLength[i] = r.width();
					}
					strmtrx[j][i] = temp;
					
				} catch (Exception e) {
				}
			}
		}
	}
	/**
	 * calls appendRows on the 3 matrices and appropriate labels.
	 * @param table
	 * @param plu
	 */
	public void plu(TableLayout table, ArrayList<com.matrix.math.Matrix> plu) 
	{
		TableRow row = new TableRow(this);
		TextView c = new TextView(this);
		ResultsDrawable r;
		com.matrix.math.Matrix m = GlobalVars.getInstance().getResultMatrices().get(0);
		setStringMatrix(m);
		c.setText("Matrix "+m.getName());
		c.setPadding(3, 3, 3, 3);
		c.setTextSize(20);
		c.setTextColor(Color.BLACK);
		//adds the textView to the row
		row.addView(c);
		table.addView(row, new TableLayout.LayoutParams());
		row = new TableRow(this);
		r = new ResultsDrawable(this, strmtrx, maxColLength);
		row.addView(r, r.width()+25, r.height());
		table.addView(row, new TableLayout.LayoutParams());
		
		m = GlobalVars.getInstance().getResultMatrices().get(1);
		setStringMatrix(m);
		c = new TextView(this);
		c.setText("Matrix "+m.getName());
		c.setPadding(3, 3, 3, 3);
		c.setTextSize(20);
		c.setTextColor(Color.BLACK);
		//adds the textView to the row
		row = new TableRow(this);
		row.addView(c);
		table.addView(row, new TableLayout.LayoutParams());
		row = new TableRow(this);
		r = new ResultsDrawable(this, strmtrx, maxColLength);
		row.addView(r, r.width()+25, r.height());
		table.addView(row, new TableLayout.LayoutParams());
		
		m = GlobalVars.getInstance().getResultMatrices().get(2);
		setStringMatrix(m);
		c = new TextView(this);
		c.setText("Matrix "+m.getName());
		c.setPadding(3, 3, 3, 3);
		c.setTextSize(20);
		c.setTextColor(Color.BLACK);
		//adds the textView to the row
		row = new TableRow(this);
		row.addView(c);
		table.addView(row, new TableLayout.LayoutParams());
		row = new TableRow(this);
		r = new ResultsDrawable(this, strmtrx, maxColLength);
		row.addView(r, r.width()+25, r.height());
		table.addView(row, new TableLayout.LayoutParams());
	}
}
 


